package com.tang.lts.core.jvmmonitor;

import com.tang.lts.core.jvmmonitor.mbean.JVMGCMBean;
import com.tang.lts.core.jvmmonitor.mbean.JVMInfoMBean;
import com.tang.lts.core.jvmmonitor.mbean.JVMMemoryMBean;
import com.tang.lts.core.jvmmonitor.mbean.JVMThreadMBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @classname JVMCollector
 * @description [  ]
 * @date 2022/5/10 14:06
 */
public class JVMCollector {

    public static JVMData getJVMData() {
        JVMData jvmData = new JVMData();
        jvmData.setGcMap(JVMMonitor.getAttributes(JVMConstants.JMX_JVM_GC_NAME, getAttributes(JVMGCMBean.class)));
        jvmData.setInfoMap(JVMMonitor.getAttributes(JVMConstants.JMX_JVM_INFO_NAME, getAttributes(JVMInfoMBean.class)));
        jvmData.setMemoryMap(JVMMonitor.getAttributes(JVMConstants.JMX_JVM_MEMORY_NAME, getAttributes(JVMMemoryMBean.class)));
        jvmData.setThreadMap(JVMMonitor.getAttributes(JVMConstants.JMX_JVM_THREAD_NAME, getAttributes(JVMThreadMBean.class)));
        return jvmData;
    }

    public static List<String> getAttributes(Class claz) {
        List<String> list = new ArrayList<>();
        for (Field field : claz.getDeclaredFields()) {
            list.add(field.getName());
        }
        return list;
    }

}